#注册指纹代码
#按两次指纹登录一个模板存于flash指纹库
#执行PS_GetImage和PS_Genchar得到特征文件存到bufferID=1
#执行PS_GetImage和PS_Genchar得到特征文件存到bufferID=2
#执行regModel生成模版
#执行StoreChar将缓冲区的文件存储到flash指纹库
import time
import serial
import string
import binascii

s=serial.Serial('/dev/ttyUSB0',115200)
GetImage = [0XEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X03,0X01,0X00,0X05]
GenChar_1= [0XEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X04,0X02,0X01,0X00,0X08]
GenChar_2= [0XEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X04,0X02,0X02,0X00,0X09]
RegModel=  [0XEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X03,0X05,0X00,0X09]
#最后的校验和要修改
StoreChar= [0XEF,0X01,0XFF,0XFF,0XFF,0XFF,0X01,0X00,0X06,0X06,0X01,0X00,0X07,0X00,0X15]
#获取指纹图像
print("GetImage")
while True:
    s.write(GetImage)
    time.sleep(1)
    number=s.inWaiting()
    read= str(binascii.b2a_hex(s.read(number)))[2:-1]
    if read[19]=='0':
        print("指纹收集成功")
        break
    else:
        print("指纹收集失败")

s.write(GenChar_1)
time.sleep(2)
number=s.inWaiting()
print(number)
read= str(binascii.b2a_hex(s.read(number)))[2:-1]
print(read)
if read[19]=='0':
    print("指纹存入缓冲区成功")
else:
    print("指纹存入缓冲区失败")
    
#获取指纹图像
print("GetImage")
while True:
    s.write(GetImage)
    time.sleep(1)
    number=s.inWaiting()
    read= str(binascii.b2a_hex(s.read(number)))[2:-1]
    if read[19]=='0':
        print("指纹收集成功")
        break
    else:
        print("指纹收集失败")

s.write(GenChar_2)
time.sleep(2)
number=s.inWaiting()
read= str(binascii.b2a_hex(s.read(number)))[2:-1]
print(read)
if read[19]=='0':
    print("指纹存入缓冲区成功")
else:
    print("指纹存入缓冲区失败")

s.write(RegModel)
time.sleep(2)
number=s.inWaiting()
read= str(binascii.b2a_hex(s.read(number)))[2:-1]
if read[19]=='0':
    print("指纹存为模版成功")
else:
    print("指纹存为模版失败")

s.write(StoreChar)
time.sleep(2)
number=s.inWaiting()
read= str(binascii.b2a_hex(s.read(number)))[2:-1]
print(read)
if read[19]=='0':
    print("指纹存到flash成功")
else:
    print("指纹存到flash失败")
s.close()
